//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pss;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.DicTranCodeDao;
import com.aidong.dao.wills.PssProductTypeCodeDao;
import com.aidong.model.wills.PssProductTypeCode;
import com.aidong.web.controller.BaseController;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"productType"})
public class ProductTypeController extends BaseController {
    @Resource
    PssProductTypeCodeDao pssProductTypeCodeDao;
    @Resource
    DicTranCodeDao dicTranCodeDao;

    public ProductTypeController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return new ModelAndView("pss/producttype/list");
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "typeCode",required = false) String typeCode, @RequestParam(value = "typeName",required = false) String typeName, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        cd.eq("typeCode", typeCode);
        cd.like("typeName", "%" + typeName + "%");
        this.pssProductTypeCodeDao.queryPageList(pg, cd, (new OrderSet()).asc("typeCode"));
        ModelAndView mv = new ModelAndView("pss/producttype/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        PssProductTypeCode productType = new PssProductTypeCode();
        ModelAndView mv = new ModelAndView("pss/productType/edit");
        mv.addObject("ppsProductTypeCode", productType);
        mv.addObject("tranCode", this.dicTranCodeDao.getListByCategory("03"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("ppsProductTypeCode") PssProductTypeCode productType, @RequestParam(value = "typeCode",required = false) String typeCode, @RequestParam(value = "typeName",required = false) String typeName, @RequestParam(value = "traCode",required = false) String traCode, @RequestParam(value = "adjCode",required = false) String adjCode) {
        if(this.chkNull(productType.getTypeCode())) {
            return this.getResult(false, "请输入类型编号");
        } else if(this.chkNull(productType.getTypeName())) {
            return this.getResult(false, "请输入类型名称");
        } else if(this.chkNull(productType.getTraCode())) {
            return this.getResult(false, "请输入科目代码");
        } else if(this.pssProductTypeCodeDao.exists(productType.getTypeCode())) {
            return this.getResult(false, "类型编号已存在");
        } else {
            productType.setStatus(Integer.valueOf(1));
            productType.setCreateUser(this.getLoginUserCode());
            productType.setCreateTime(new Date());
            productType.setUpdateTime(new Date());

            try {
                this.pssProductTypeCodeDao.add(productType);
                this.reloadTypeList("productType");
                this.addUserLog("商品类型设置", "编号:" + productType.getTypeCode() + ",名称:" + productType.getTypeName());
            } catch (Exception var7) {
                this.log.error("add PpsProductCode", var7);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "保存完成");
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("typeCode") String typeCode) {
        PssProductTypeCode productType = (PssProductTypeCode)this.pssProductTypeCodeDao.getById(typeCode);
        if(productType == null) {
            return this.showAlert("没有这个记录");
        } else {
            ModelAndView mv = new ModelAndView("pss/productType/edit");
            mv.addObject("ppsProductTypeCode", productType);
            mv.addObject("tranCode", this.dicTranCodeDao.getListByCategory("03"));
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("productType") PssProductTypeCode productType) {
        if(this.chkNull(productType.getTypeCode())) {
            return this.getResult(false, "请输入类型编号");
        } else if(this.chkNull(productType.getTypeName())) {
            return this.getResult(false, "请输入类型名称");
        } else {
            PssProductTypeCode productType1 = (PssProductTypeCode)this.pssProductTypeCodeDao.getById(productType.getTypeCode());
            if(productType1 == null) {
                return this.getResult(false, "没有这个记录");
            } else {
                productType1.setTypeName(productType.getTypeName());
                productType1.setNote(productType.getNote());
                productType1.setTraCode(productType.getTraCode());
                productType1.setUseGiftCard(productType.getUseGiftCard());
                productType1.setUpdateTime(new Date());

                try {
                    this.pssProductTypeCodeDao.update(productType1);
                    this.reloadTypeList("productType");
                    this.addUserLog("修改商品类型设置", "编号:" + productType1.getTypeCode() + ",名称:" + productType1.getTypeName());
                } catch (Exception var4) {
                    this.log.error("edit pssProductType", var4);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("typeCode") String typeCode) {
        PssProductTypeCode productType = (PssProductTypeCode)this.pssProductTypeCodeDao.getById(typeCode);
        if(productType == null) {
            return this.getResult(false, "没有这个记录");
        } else {
            productType.setUpdateTime(new Date());
            productType.setStatus(Integer.valueOf(0));

            try {
                this.pssProductTypeCodeDao.update(productType);
                this.reloadTypeList("productType");
                this.addUserLog("删除商品类型设置", "编号:" + productType.getTypeCode() + ",名称:" + productType.getTypeName());
            } catch (Exception var4) {
                this.log.error("del pssProductType", var4);
                return this.getResult(false, "删除发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }
}
